package com.atguigu05.method_more._01overload.exer._04recursion;

/**
 * ClassName: RecursionTest
 * Description:
 *递归方法
 * @Author liu
 * @Create 2024/8/22 15:08
 * @Version 1.0
 */

public class RecursionTest {
    public static void main(String[] args) {
        RecursionTest test = new RecursionTest();
        System.out.println(test.getSum1(100));
        System.out.println(test.getSum2(100));
        System.out.println(test.getMul(4));
        System.out.println(test.f(10));

    }
    /*
     * 举例1：计算1-100内自然数的总和
     * */
    public int getSum1(int num){
        int sum = 0;
        for (int i = 0; i <= num ; i++) {
            sum += i;

        }
        return sum;

    }

    public int getSum2(int num){
        if(num == 1){
            return 1;
        }else{
            return getSum2(num -1) + num;
        }


    }

    /**
     * 举例2：计算n!
     */
    public int getMul(int n){
        if(n == 1){
            return 1;
        }else{
            return n * getSum2(n -1);
        }
    }

    /**
     * * 举例5：斐波那契数列
     *     1  1  2    3   5   8   13  21  34  55  ...
     *    f(n) = f(n-1)+f(n-2)
     */
    public int f(int n){//n 为第几个
        if(n == 1){
            return 1;
        }else if(n == 2){
            return 1;
        } else{
            return f(n-1)+f(n-2);
        }
    }

}